package cn.xttc.edu.controller;

import cn.xttc.edu.service.CarService;
import cn.xttc.edu.service.CustomerService;
import cn.xttc.edu.service.DriverService;
import cn.xttc.edu.util.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跳转页面的controller
 * @author yl
 */
@Controller
public class MainController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private CarService carService;

    @GetMapping("/loginPage")
    public String loginPage(){
        return "login";
    }

    @GetMapping("/")
    public String login(){
        return "redirect:/indexPage";
    }

    @GetMapping("/indexPage")
    public String index() {
        return "index";
    }

    @GetMapping("/mainPage")
    public String main() {
        return "main";
    }

    @GetMapping("/adminPage")
    public String adminPage() {
        return "admin";
    }

    @GetMapping("/rulePage")
    public String rule() {
        return "rule";
    }

    @GetMapping("/carTypePage")
    public String carType() {
        return "car_type";
    }

    @GetMapping("/carPage")
    public String car(@RequestParam(value = "carId",required = false, defaultValue = "") String carId, HttpServletRequest request) {
        String licence = "";
        if (!"".equals(carId)) {
            licence = carService.getById(carId).getLicence();
        }
        request.setAttribute("licence", licence);
        return "car";
    }

    @GetMapping("/ordersPage_waiting")
    public String ordersWaiting() {
        return "orders_waiting";
    }

    @GetMapping("/ordersPage_going")
    public String ordersGoing() {
        return "orders_going";
    }

    @GetMapping("/ordersPage_completed")
    public String ordersCompleted() {
        return "orders_completed";
    }

    @GetMapping("/ordersPage_cancelled")
    public String ordersCancelled() {
        return "orders_cancelled";
    }

    /**
     * 跳转至客户管理页面，如果cNameId有值就直接查询出来对应的客户信息
     * @param cNameId 客户id
     * @param request request对象
     * @return 客户管理页面
     */
    @GetMapping("/customerPage")
    public String customer(@RequestParam(value = "cNameId", required = false, defaultValue = "")String cNameId, HttpServletRequest request) {
        String cName = "";
        if (!"".equals(cNameId)) {
            cName = customerService.getById(cNameId).getName();
        }
        request.setAttribute("cName", cName);
        return "customer";
    }

    @GetMapping("/driver_registered")
    public String driverRegistered(@RequestParam(value = "dId", required = false, defaultValue = "") String dId, HttpServletRequest request) {
        String dName = "";
        if (!"".equals(dId)) {
            dName = driverService.getById(dId).getName();
        }
        request.setAttribute("dName", dName);
        return "driver_registered";
    }

    @GetMapping("/driver_unregistered")
    public String driverUnregistered() {
        return "driver_unregistered";
    }

    @GetMapping("/wx/driver_add")
    public String driverAdd(String openid, HttpServletRequest request) {
        request.setAttribute("openid", openid);
        return "wx/driver_add";
    }

    @GetMapping("/wx/driver")
    public String driver() {
        return "wx/driver";
    }

    @GetMapping("/wx/driver_detail")
    public String driverDetail(@RequestParam("openid")String openid, HttpServletRequest request) {
        request.setAttribute("openid", openid);
        return "wx/driver_detail";
    }

    @GetMapping("/wx/customer")
    public String customer() {
        return "wx/customer";
    }

    @GetMapping("/wx/order_detail")
    public String orderDetail(
            @RequestParam("id")String id,
            @RequestParam("status")Integer status,
            @RequestParam(value = "dId", required = false, defaultValue = "")String driverId,
            HttpServletRequest request)
    {
        request.setAttribute("order_id", id);
        request.setAttribute("order_status", status);
        request.setAttribute("dId", driverId);
        return "wx/order_detail";
    }

    /**
     * 获取生成验证码显示到 UI 界面
     * @param request request
     * @param response response
     * @throws ServletException servlet exception
     * @throws IOException io exception
     */
    @GetMapping(value="/checkCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandomCode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/editPwd")
    public String editPwd () {
        return "edit_pwd";
    }

    @GetMapping("/profilePage")
    public String profile() {
        return "profile";
    }
}
